//部分控制器
var app = angular.module('myApp.controllers');

//example首页
app.controller('orderIndex', ["$scope", "utilService", "$rootScope", '$ionicScrollDelegate', function ($scope, utilService, $rootScope, $ionicScrollDelegate) {
  /*模拟数据*/
  $scope.orderList = [{id:0},{id:1},{id:2},{id:3}];

  /*点击详情*/
  $scope.showDetail = function (item) {
    utilService.$state.go('order.detail', {id:item.id});
  }
  // tab切换 如果需要无限加载加这个 不需要就算了。
  $scope.integralParams = {
    limit: 10,
    page: -1
  }
  // 当前参数
  $scope.data = {
    id: "", //当前选中tab的ID
    top: 0,//当前选中tab滚动的位置
    isLoad: false,//当前选中tab是否是第一次load
    currPage: 0,//当前选中tab当前第几页
    hasMore: true,
    posts: [],//当前选中tab的数据
  };
  $scope.tabs = ["全部", "代付款", "待完成", "待评价", "退款售后"];
  $scope.tabMap = {
    "全部": {
      currPage: -1,
      posts: [],
      isLoad: true,
      hasMore: true,
      id: "全部",
      top: 0
    },
    "代付款": {
      currPage: -1,
      posts: [],
      isLoad: true,
      hasMore: true,
      id: "代付款",
      top: 0
    },
    "待完成": {
      currPage: -1,
      posts: [],
      isLoad: true,
      hasMore: true,
      id: "待完成",
      top: 0
    },
    "待评价": {
      currPage: -1,
      posts: [],
      isLoad: true,
      hasMore: true,
      id: "待评价",
      top: 0
    },
    "退款售后": {
      currPage: -1,
      posts: [],
      isLoad: true,
      hasMore: true,
      id: "退款售后",
      top: 0
    }
  };
  $scope.isActive = function(item) {
    return item == $scope.data.id;
  }

  //标签切换
  $scope.switchTab = function (id) {
    var pos = $ionicScrollDelegate.getScrollPosition();
    pos && ($scope.data.top = pos.top);

    var map = $scope.tabMap[id];
    if (!map) {
      map = $scope.tabMap[id] = {};
    }

    $scope.data = map;
    $scope.integralParams.page = map.currPage;
    $ionicScrollDelegate.resize();
    !map.top && (map.top = 0);
    $ionicScrollDelegate.scrollTo(0, map.top || 0, false);
  };
  $scope.switchTab($scope.tabs[utilService.$stateParams.type]); //默认选择第一个





  $scope.$on('$ionicView.beforeEnter', function () {
        //页面刚加载
        utilService.$ionicLoading.show(HX_CONFIG.loadingBase);
    }, false);
    $scope.$on('$ionicView.afterEnter', function () {
        utilService.$rootScope.rootData.title = '我的订单';
        //页面加载完成
        utilService.$ionicLoading.hide();
    }, false);
    $rootScope.$on('$ionicView.beforeLeave', function (e, ion) {
        //页面离开之前
        //alertPopup && alertPopup.close();

    }, false);

}]);
app.controller('orderDetail', ["$scope", "utilService", "$rootScope", '$ionicScrollDelegate', function ($scope, utilService, $rootScope, $ionicScrollDelegate) {
  utilService.$ionicModal.fromTemplateUrl('order-modal.html', {
    scope: $scope,
    animation: 'slide-in-up'
  }).then(function(modal) {
    $scope.modal = modal;
  });
  $scope.closeModal = function() {
    $scope.modal.hide();
  };
  // Cleanup the modal when we're done with it!
  $scope.$on('$destroy', function() {
    $scope.modal.remove();
  });
  // Execute action on hide modal
  $scope.$on('modal.hidden', function() {
    // Execute action
  });
  // Execute action on remove modal
  $scope.$on('modal.removed', function() {
    // Execute action
  });
  /*查看订单状态*/
  $scope.showOrder = function () {
      $scope.modal.show();

  }
  /*点击评价*/
  $scope.addComment = function () {
    utilService.$state.go('order.comment', {id:utilService.$stateParams.id});
  }

  $scope.$on('$ionicView.beforeEnter', function () {
    //页面刚加载
    utilService.$ionicLoading.show(HX_CONFIG.loadingBase);
  }, false);
  $scope.$on('$ionicView.afterEnter', function () {
    utilService.$rootScope.rootData.title = '订单详情';
    //页面加载完成
    utilService.$ionicLoading.hide();
  }, false);
  $rootScope.$on('$ionicView.beforeLeave', function (e, ion) {
    //页面离开之前
    //alertPopup && alertPopup.close();

  }, false);
}]);
app.controller('orderComment', ["$scope", "utilService", "$rootScope", function ($scope, utilService, $rootScope) {
  $scope.toComment = function () {
    utilService.$state.go('order.success', {id:utilService.$stateParams.id});
  }

  $scope.$on('$ionicView.beforeEnter', function () {
    //页面刚加载
    utilService.$ionicLoading.show(HX_CONFIG.loadingBase);
  }, false);
  $scope.$on('$ionicView.afterEnter', function () {
    utilService.$rootScope.rootData.title = '发表评价';
    //页面加载完成
    utilService.$ionicLoading.hide();
  }, false);
  $rootScope.$on('$ionicView.beforeLeave', function (e, ion) {
    //页面离开之前
    //alertPopup && alertPopup.close();

  }, false);
}]);
app.controller('orderSuccess', ["$scope", "utilService", "$rootScope", function ($scope, utilService, $rootScope) {


  $scope.$on('$ionicView.beforeEnter', function () {
    //页面刚加载
    utilService.$ionicLoading.show(HX_CONFIG.loadingBase);
  }, false);
  $scope.$on('$ionicView.afterEnter', function () {
    utilService.$rootScope.rootData.title = '评价成功';
    //页面加载完成
    utilService.$ionicLoading.hide();
  }, false);
  $rootScope.$on('$ionicView.beforeLeave', function (e, ion) {
    //页面离开之前
    //alertPopup && alertPopup.close();

  }, false);
}]);